<?php
class admin extends spController
{
	function __construct()
	{
	        // 必须加入启动父类构造函数的操作
	        parent::__construct();
	         
	        // 开始全局操作
	        $this->arr_cat = Product_Cat();
			$this->a = $_GET['a'];
			$this->c = $_GET['c'];
			
	}

	function post()
	{
		$m_youhui = spClass("youhui");
		$m_attachment = spClass('attachment');

		if(!empty($_POST)) {
			$verifier = $m_youhui->spVerifier($_POST);
			if( false != $verifier) {
				$this->error('提交数据不正确', spUrl('admin', 'post'));
			} else {
				$_POST['thumb'] = $m_attachment->upload('thumb');
				if(empty($_POST['thumb'])) {
					$this->error('提交数据不正确', spUrl('admin', 'post'));
				}
				if($_POST['catid'] <= 0) {
					$this->error('提交数据不正确', spUrl('admin', 'post'));
				}
				$_POST['createtime'] = date('Y-m-d H:i:s', time());
			
				if($_POST['url'] && strpos($_POST['url'], '?')) {
						$url = substr($_POST['url'], strpos($_POST['url'], '?')+1);
						parse_str($url, $arr_url);
						if(!in_array($arr_url['p'], array('mm_30161668_0_0', 'mm_29938918_0_0')) && !empty($arr_url['p'])) $this->error('提交的pid不正确', spUrl('admin', 'edit', array('pid'=>$_POST['pid'])));
				}

				$pid = $m_youhui->create($_POST);
				if($_POST['posid']) {
					$arr_pos[$pid] = $_POST['posid'];
					$this->position($arr_pos);
				}
				$this->success('提交成功', spUrl('admin', 'listinfo'));
			}
		} else {
			if($_GET['store_id']) {
				$store_id = $_GET['store_id'];
				$m_store = spClass("m_store");
				$this->store_result = $m_store->find(array('store_id'=>$store_id));
			}
			$this->display("admin/admin_post.html");
		}
	}

	function edit()
	{
		$m_youhui = spClass("youhui");
		$m_attachment = spClass('attachment');
		$arr_pos = array();
		if(!empty($_POST)) {
			$verifier = $m_youhui->spVerifier($_POST);
			if( false != $verifier) {
				$this->error('提交数据不正确');
			} else {
				if($_POST['catid'] <= 0) {
					$this->error('提交的pid不正确', spUrl('admin', 'edit', array('pid'=>$_POST['pid'])));
				}
				$pid = $_POST['pid'];
				unset($_POST['pid']);
				if($_FILES['thumb']['size'] > 0) {
					$_POST['thumb'] = $m_attachment->upload('thumb');
				}				
				$_POST['status'] = $_POST['status'] == 1 ? 1 : 0;
				$_POST['updatetime'] = date('Y-m-d H:i:s', time());
			
				if($_POST['url'] && strpos($_POST['url'], '?')) {
						$url = substr($_POST['url'], strpos($_POST['url'], '?')+1);
						parse_str($url, $arr_url);
						if(!in_array($arr_url['p'], array('mm_30161668_0_0', 'mm_29938918_0_0')) && !empty($arr_url['p'])) $this->error('提交的pid不正确', spUrl('admin', 'edit', array('pid'=>$_POST['pid'])));
				}
				$m_youhui->update(array('pid'=>$pid), $_POST);
				if($_POST['posid']) {
					$arr_pos[$pid] = $_POST['posid'];
					$this->position($arr_pos);
				}
				$this->success('修改成功', spUrl('admin', 'listinfo'));
			}
		} else {
			$pid = intval($_GET['pid']);
			$this->result = $m_youhui->find(array('pid'=>$pid));
			$m_store = spClass("m_store");
			$this->store_result = $m_store->find(array('store_id'=>$this->result['store_id']));
			$this->display("admin/admin_editpost.html");
		}
	}

	function listinfo()
	{
		$m_youhui = spClass("youhui");
		$condition = " isdel=0 AND enddate>=curdate()";
		$this->results = $m_youhui->spPager($this->spArgs('page', 1), 10)->findAll($condition, 'listorder DESC, pid DESC');
		$this->pager = $m_youhui->spPager()->getPager();
		$this->display("admin/admin_list.html");
	}

	function get_also_bought()
	{
		$m_youhui = spClass("youhui");
		$pid = intval($_GET['pid']);
		if($pid < 1) return false;
		$sql = "SELECT * FROM `click` AS a LEFT JOIN `click` b ON a.visitor_id=b.visitor_id LEFT JOIN `youhui` c ON b.pid=c.pid WHERE a.pid='$pid' AND b.pid<>'$pid' AND a.visitor_id!=0 AND status=0 AND isdel=0 AND enddate>=curdate() GROUP BY b.pid ORDER BY nums DESC limit 5";
		$this->results = $m_youhui->findSql($sql);
		$this->display("admin/admin_list.html");
	}

	/*
	 *	获得
	 */
	function todaylist()
	{
		$m_youhui = spClass("youhui");
		$sql = "SELECT * FROM `youhui` WHERE `startdate`=curdate()";
		$this->results = $m_youhui->findSql($sql);
		$this->display("admin/admin_list.html");
	}

	function search()
	{
		$m_youhui = spClass("youhui");
		$condition = " isdel=0 ";
		$this->title = $title = $_GET['title'];
		if(!empty($title)) {
			$condition .= " AND title LIKE '%$title%'";
		}
		$this->store_id = $store_id = $_GET['store_id'];
		if(!empty($store_id)) {
			$condition .= " AND store_id='$store_id'";
		}
		$enddate = $_GET['enddate'];
		if(!empty($enddate)) {
			$condition .= " AND enddate='$enddate'";
		}
		$this->results = $m_youhui->spPager($this->spArgs('page', 1), 50)->findAll($condition);
		$this->pager = $m_youhui->spPager()->getPager();
		$this->display("admin/admin_list.html");
	}

	function listinfo_old()
	{
		$m_youhui = spClass("youhui");
		$condition = " isdel=0 AND enddate<curdate()";
		$this->results = $m_youhui->spPager($this->spArgs('page', 1), 10)->findAll($condition, 'listorder DESC, pid DESC');
		$this->pager = $m_youhui->spPager()->getPager();
		$this->display("admin/admin_list.html");
	}

	function status()
	{
		$m_youhui = spClass("youhui");
		if(!empty($_POST)) {
			$pid = intval($_POST['pid']);
			$set = intval($_POST['status']);		
			$m_youhui->update(array('pid'=>$pid), array('status'=>$set, 'updatetime'=>date('Y-m-d H:i:s', time())));
			$m_log = spClass('m_log');
			$m_log->create(array('action'=>'status', 'data'=>$set, 'content'=>$_POST['content']));
			$this->success('操作成功', spUrl('admin', 'listinfo'));
		} else {
			$pid = intval($_GET['pid']);
			if($pid < 1) return false;
			$this->result = $m_youhui->find(array('pid'=>$pid));
			$this->display("admin/post_status.html");
		}
	}

	function del()
	{
		$m_youhui = spClass("youhui");
		$pid = intval($_GET['pid']);
		$m_youhui->update(array('pid'=>$pid), array('isdel'=>1));
		$this->success('删除成功', spUrl('admin', 'listinfo'));
	}

	function listorder()
	{
		$m_youhui = spClass("youhui");
		if(!empty($_POST)) {
			foreach($_POST['listorder'] as $k=>$v) {
				$m_youhui->update(array('pid'=>$k), array('listorder'=>$v));
			}
			$this->success('排序成功', spUrl('admin', 'listinfo'));
		}
	}

	function trash()
	{
		$m_youhui = spClass("youhui");
		$this->results = $m_youhui->spPager($this->spArgs('page', 1), 10)->findAll(array('isdel'=>1), 'listorder DESC, pid DESC');
		$this->pager = $m_youhui->spPager()->getPager();
		$this->display("admin_trash.html");
	}

	function recover()
	{
		$m_youhui = spClass("youhui");
		$pid = intval($_GET['pid']);
		$m_youhui->update(array('pid'=>$pid), array('isdel'=>0));
		$this->success('恢复成功', spUrl('admin', 'listinfo'));
	}

	/*
	 *	将活动推荐到推荐位
	 */
	function position($arr_pos)
	{
		$m_position = spClass('m_position');
		$pid = key($arr_pos);
		$posid = $arr_pos[$pid];
		$condition = array('pid'=>$pid, 'posid'=>$posid);
		if($m_position->find($condition)) {
					return false;
		}

		$m_position->create($condition);
		return true;
		/*
		foreach($arr_pos as $pid=>$arr_posid) {
			foreach($arr_posid as $posid) { 
				$condition = array('pid'=>$pid, 'posid'=>$posid);
		
				
				if($m_position->find(array('pid'=>$pid, 'posid'=>$posid))) {
					continue;
				}
				$m_position->create($condition);
			}
		}
		*/
	}
}
?>